<?php

require_once 'doctrine-orm/Doctrine/Common/Persistence/ObjectRepository.php';
require_once 'doctrine-orm/Doctrine/ORM/EntityRepository.php';

use Doctrine\ORM\EntityRepository,
    Doctrine\Common\Persistence\ObjectRepository;

/**
 * Description of PlayerDAO
 *
 * @author alexey.ogarkov
 */
class PlayerRepository extends EntityRepository{
    public function findPlayerById($id) {
        return find("Player", $id);
    }
    
    public function findPlayerByFacebookId($facebookId) {
        $dql = "SELECT p FROM Player p WHERE p.facebookId = ?1";
        return $this->getEntityManager()
                ->createQuery($dql)                
                ->setParameter(1, $facebookId)
                ->getSingleResult();
    }
    
    public function createPlayer($userId,$name) {
        $player = new Player();
        $player->setFacebookId($userId);
        $player->setName($name);
        
        $this->getEntityManager()->persist($player);
        $this->getEntityManager()->flush();
        
        return $player;
    }
}

?>
